# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=cfitsio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.380
pkgrel=1
pkgdesc="A library of C and Fortran subroutines for reading and writing data files in FITS (Flexible Image Transport System) data format (mingw-w64)"
arch=('any')
url="https://heasarc.gsfc.nasa.gov/fitsio/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('strip' 'staticlibs')
source=("https://heasarc.gsfc.nasa.gov/FTP/software/fitsio/c/${_realname}${pkgver/./}.tar.gz"
        mingw-shared.patch)
sha256sums=('0ff4b43365ea995274696e75c8bad4e58c7a077cdc253d235f2bc93b3715b197'
            '6c9bc7db7a3f3a356daa19f796fb639c33e6075174ed5537fe7ff12c14333312')

prepare() {
  cd ${_realname}
  patch -p1 -i ${srcdir}/mingw-shared.patch
  autoreconf -fi
}

build() {
  #export lt_cv_deplibs_check_method='pass_all'
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  cp -rf ${_realname} build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}

  make shared
}

check() {
  cd build-${MINGW_CHOST}
  make testprog
  LD_LIBRARY_PATH=. ./testprog > testprog.lis
  [[ -z $(diff testprog.lis testprog.out) ]] || return 1
  [[ -z $(cmp testprog.fit testprog.std) ]] || return 1
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/bin
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  install -Dm644 License.txt "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
